package com.qianli.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.dao.UserAccessDao;
import com.fqgj.xjd.user.dao.UserBehaviorRegisterDao;
import com.fqgj.xjd.user.dao.UserDao;
import com.fqgj.xjd.user.entity.UserAccessEntity;
import com.fqgj.xjd.user.entity.UserEntity;
import com.google.common.collect.Lists;
import com.qianli.user.ro.Paged;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.account.UserAccessRO;
import com.qianli.user.ro.account.UserRO;
import com.qianli.user.ro.auth.CustomerRO;
import com.qianli.user.ro.query.UserConditionQueryRO;
import com.qianli.user.ro.query.UserQueryRO;
import com.qianli.user.service.UserQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * name:用户查询
 * auth:ting.li@qianli-inc.com
 * date:2018/1/9 17:50
 * version: v1.0.0
 **/
@Service
public class UserQueryServiceImpl implements UserQueryService{

    @Autowired
    private UserBehaviorRegisterDao userBehaviorRegisterDao;

    @Autowired
    private UserAccessDao userAccessDao;

    @Autowired
    private UserDao userDao;


    /**
     * 按注册来源分页查询用户信息
     * @param queryRO
     * @return
     */
    @Override
    public Paged<List<UserAccessRO>> pagedQueryUserByRegisterFrom(UserConditionQueryRO queryRO){
        Integer total = userBehaviorRegisterDao.countTotalByRegisterFrom(queryRO.getRegisterFrom(),
                queryRO.getAppCode(),queryRO.getBiz());
        Integer start = (queryRO.getCurrentPage()-1)*queryRO.getPageSize();
        List<String> userCodes = userBehaviorRegisterDao.pagedQueryUserByRegisterFrom(queryRO.getRegisterFrom(),
                queryRO.getAppCode(),queryRO.getBiz(),start,queryRO.getPageSize());
        //为空
        Paged<List<UserAccessRO>> paged = null;
        if(CollectionUtils.isEmpty(userCodes)){
            paged = new Paged<>(total,queryRO.getPageSize(),queryRO.getCurrentPage(),null);
            return paged;
        }
        //查询access信息
        List<UserAccessEntity> accessEntities = userAccessDao.selectListByUserCodes(userCodes);
        List<UserAccessRO> accessROS = Lists.newArrayList();
        for(UserAccessEntity entity:accessEntities){
            UserAccessRO accessRO = new UserAccessRO();
            BeanUtils.copyProperties(entity,accessRO);
            accessRO.setAppCode(queryRO.getAppCode());
            accessROS.add(accessRO);
        }
        paged = new Paged<>(total,queryRO.getPageSize(),queryRO.getCurrentPage(),accessROS);
        return paged;
    }

    /**
     * 根据身份证查询用户信息
     * @param query
     * @return
     */
    @Override
    public Response<UserRO> getUserByIdentityNo(UserQueryRO query){

        UserEntity userEntity = userDao.selectByIdentityNoAndAppCode(query.getIdentityNo(),query.getAppCode(),query.getBiz());
        if(null == userEntity){
            return Response.error(UserStateCode.USER_NOT_EXITS.getMsg());
        }
        UserRO userRO = new UserRO();
        CustomerRO customerRO = new CustomerRO();
        BeanUtils.copyProperties(userEntity,customerRO);
        userRO.setCustomer(customerRO);
        List<UserAccessEntity> accessEntities = userAccessDao.selectByCustomerCode(userEntity.getCustomerCode());
        UserAccessEntity userAccessEntity = accessEntities.get(0);
        userRO.setUserCode(userAccessEntity.getUserCode());
        UserAccessRO accessRO = new UserAccessRO();
        BeanUtils.copyProperties(userAccessEntity,accessRO);
        userRO.setUserAccess(accessRO);
        return Response.ok(userRO);
    }

    /**
     * 查询实名信息
     * @param customerCode
     * @return
     */
    @Override
    public Response<CustomerRO> queryUserByCustomerCode(String customerCode) {
        UserEntity userEntity = userDao.selectByCustomerCode(customerCode);
        if(null == userEntity){
            return Response.ok();
        }
        CustomerRO customerRO = new CustomerRO();
        BeanUtils.copyProperties(userEntity,customerRO);
        return  Response.ok(customerRO);
    }

    /**
     * 查询实名信息
     * @param userCode
     * @return
     */
    @Override
    public Response<UserRO> queryUserByUserCode(String userCode) {

        UserAccessEntity userAccess = userAccessDao.selectUserAccessByUserCode(userCode);
        if(null == userAccess){
            return Response.error(UserStateCode.USER_NOT_EXITS.getMsg());
        }
        UserEntity userEntity = userDao.selectByCustomerCode(userAccess.getCustomerCode());
        UserRO user = new UserRO();
        user.setUserCode(userCode);
        UserAccessRO userAccessRO = new UserAccessRO();
        BeanUtils.copyProperties(userAccess,userAccessRO);
        user.setUserAccess(userAccessRO);
        if(null != userEntity){
            CustomerRO customerRO = new CustomerRO();
            BeanUtils.copyProperties(userEntity,customerRO);
            user.setCustomer(customerRO);
        }
        return  Response.ok(user);
    }
}
